package com.wisecoach.gatewayplus.transaction;

/**
 * 默认实现，根据不同策略生成不同的事务上下文
 * {@code @author:} wisecoach
 * {@code @date:} 2023/3/3 下午8:04
 * {@code @version:} 1.0.0
 */


public abstract class AbstractTransactionContextProvider implements TransactionContextProvider {

    @Override
    public TransactionContext getTransactionContext(TransactionAttribute attribute) {
        TransactionStatus status = getTransaction(attribute);
        TransactionContext context = new DefaultTransactionContext(status);
        TransactionContext oldTransaction = TransactionContextHolder.getContext();
        if (attribute.getPropagation() != TransactionDefinition.PROPAGATION_REQUIRED) {
            throw new RuntimeException("暂时不支持该事务传播等级");
        }
        context.setOldTransaction(oldTransaction);
        return context;
    }

    protected abstract TransactionStatus getTransaction(TransactionAttribute attribute);
}
